#include "glmapwidget.h"

GlMapWidget::GlMapWidget()
{
    _emfGrid = new EmfGrid();
}

void GlMapWidget::mouseReleaseEvent(QMouseEvent *event)
{
    _lastPos = event->pos();
    _selectedItem = -1;
    QPointF selectedPoint = _projectWindowCoordinates(_lastPos);
    for(int i=0; i<_drawables.size(); i++)
    {
        Selectable* selectItem = dynamic_cast<Selectable*>(_drawables[i]);
        if(selectItem->isSelected(selectedPoint))
        {
            _selectedItem = i;
            emit selectedItem(_selectedItem);
            break;
        }
    }
    updateGL();
}

void GlMapWidget::paintGL()
{
    GLWidget::paintGL();

    _emfGrid->draw(this);

    _grid.draw(this);
    for(int i=0; i<_drawables.size(); i++)
    {
        _drawables[i]->resetColor();
        if(i == _selectedItem)
        {
            _drawables[i]->setColor(QColor(0, 255, 0));
        }
        _drawables[i]->draw(_currentCoordsTransformer);
    }
}

void GlMapWidget::selectObject(int id)
{
    _selectedItem = -1;
    for(int i=0; i<_drawables.count(); i++)
    {
        if(_drawables[i]->getTag() == id)
        {
            _selectedItem = i;
            break;
        }
    }
}


